package cn.xs.surveypark.struts2.action;

import java.net.URI;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;

import cn.xs.surveypark.domain.User;
import cn.xs.surveypark.service.UserService;
import cn.xs.surveypark.util.DataUtil;

/**
 * LoginAction 
 * @author Administrator
 */
@Controller
@Scope("prototype")
public class LoginAction extends BaseAction<User> implements SessionAware{

	/**
	 * 反序列化时候使用
	 */
	private static final long serialVersionUID = -7750684849832682442L;
	
	@Resource
	private UserService userService;

	//接受Session的map
	private Map<String,  Object> sessionMap;
	
	/**
	 * 进入登录页面
	 * @return
	 */
	public String toLoginPage(){
		return "loginPage";
	}
	
	
	
	/**
	 * 登录
	 */
	public String doLogin(){
		return SUCCESS;
	}
	
	/**
	 * 该方法只在doLogin之前运行
	 */
	public void validateDoLogin() {
		// TODO Auto-generated method stub
		User user = userService.validateLoginInfo(model.getEmail(), DataUtil.md5(model.getPassword()));
		if(user == null){
			addActionError("email/password wrong");
			return;
		}else{//登录成功的数据存储在session中
			//第一种，使用Tomcat下面的jar包
//			HttpSession s = ServletActionContext.getRequest().getSession();
//			s.setAttribute("user", user);
			
			//第二种，使用SessionAware
			sessionMap.put("user", user);
		}
		super.validate();
	}



	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.sessionMap = arg0;
	}	
}
